package com.mayi.crm.controller;

import com.mayi.crm.base.BaseController;
import com.mayi.crm.base.ResultInfo;
import com.mayi.crm.model.Role;
import com.mayi.crm.query.RoleQuery;
import com.mayi.crm.service.RoleService;
import com.mayi.crm.vo.XmSelectVo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author yacong_liu
 * @version 1.0
 * @classname RoleController
 * @description 角色控制层
 * @date 2021/1/23
 **/
@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;

    /**
     * @param []
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @description 查询所有角色
     * @date 2021/1/23 13:50
     **/
    @GetMapping("queryAllRoles")
    @ResponseBody
    public List<XmSelectVo> queryAllRoles(Integer userId) {
        List<Map<String, Object>> list = roleService.queryAllRoles(userId);
        Iterator<Map<String, Object>> iterator = list.iterator();
        List<XmSelectVo> datas = new ArrayList<XmSelectVo>();
        while (iterator.hasNext()) {
            Map<String, Object> map = iterator.next();
            XmSelectVo vo = new XmSelectVo();
            vo.setName((String) map.get("roleName"));
            vo.setValue(map.get("id").toString());
            vo.setSelected(((Long)map.get("selected") == 1)?true:false);

            datas.add(vo);
        }
        return datas;
    }

    @GetMapping("list")
    @ResponseBody
    public Map<String, Object> roleList(RoleQuery query){
        return roleService.queryByParamsForTable(query);
    }

    @PostMapping("add")
    @ResponseBody
    public ResultInfo addRole(Role role) {
        roleService.addRole(role);
        return success("角色添加成功！");
    }

    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateRole(Role role) {
        roleService.updateRole(role);
        return success("角色修改成功！");
    }

    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteRole(Integer roleId) {
        roleService.deleteRole(roleId);
        return success("角色删除成功！");
    }

    /**
     * @description 角色授权
     * @param [roleId, mIds]
     * @return com.mayi.crm.base.ResultInfo
     * @date 2021/1/28 09:14
     **/
    @PostMapping("addGrant")
    @ResponseBody
    public ResultInfo addGrant(Integer roleId, Integer[] mIds) {
        roleService.addGrant(roleId, mIds);
        return success("角色授权成功！");
    }

    @RequestMapping("index")
    public String index(){
        return "role/role";
    }

    @RequestMapping("toAddOrUpdateRolePage")
    public String toAddOrUpdateRolePage(Integer roleId, HttpServletRequest request) {
        // 如果roleId不为空，则表示修改操作，通过角色ID查询角色记录，存到请求域中
        if (roleId != null) {
            // 通过角色ID查询角色记录
            Role role = roleService.selectByPrimaryKey(roleId);
            // 设置到请求域中
            request.setAttribute("role", role);
        }
        return "role/add_update";
    }

    @RequestMapping("toAddGrantPage")
    public String toAddGrantPage(Integer roleId, Model model){
        model.addAttribute("roleId",roleId);
        return "role/grant";

    }

}
